package com.zds.scm.web.servlet;

import static com.zds.scm.common.util.StringUtils.*;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zds.scm.common.CommonServlet;
import com.zds.scm.common.service.ServiceException;
import com.zds.scm.domain.UserInfo;
import com.zds.scm.service.UserInfoService;
import com.zds.scm.service.impl.UserInfoServiceImpl;

public class UserInfoServlet extends CommonServlet {
	
	private static final long serialVersionUID = -7656323917520658642L;
	private UserInfo userInfo=new UserInfo();
	private UserInfoService userInfoService=new UserInfoServiceImpl();
	/**
	 * 此方法专门用户从请求中获取传递过来的参数,并填充对象，可能还会做类型转换,验证
	 */
	public void SetUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("1.获取表单参数");
		logger.debug("2.按照domain对象的要求做类型转换");
		logger.debug("3.填充domain对象");
		logger.debug("4.验证参数是否符合要求");
		userInfo.setUserNewPwd1(request.getParameter("userNewPwd1"));
		userInfo.setUserNewPwd2(request.getParameter("userNewPwd2"));
		userInfo.setUserCode(request.getParameter("userCode"));
		userInfo.setUserPwd(request.getParameter("userPwd"));//修改密码时存老密码
		userInfo.setUserType(request.getParameter("userType"));
		if (isNull(userInfo.getUserCode()) || isNull(userInfo.getUserPwd())) {
			logger.debug("4.验证参数是否符合要求,参数不符合要求");
			throw new ServiceException("账号、密码都不能为空");
		}
	}
	public void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行insert方法");
		//TODO 增加用户信息,并根据增加成功与否,执行相应的跳转
		//重定向到上下文路径中的登陆页面,转发时地址栏的信息会改变,request.setAttribute("", "");中设置的值，不能在重定向的页面访问
		//response.sendRedirect(request.getContextPath()+"/");  
		//转发到当前上下文路径的登陆页面,转发时地址栏的信息不变,request.setAttribute("", "");中设置的值，在可以在转发的页面中访问
		request.getRequestDispatcher("/").forward(request, response);
	}
	public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行delete方法");
		//TODO 删除指定用户,根据成功与否,执行相应的跳转
	}
	public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行update方法");
		//TODO 更新指定用户,根据成功与否,执行相应的跳转
	}
	public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行update方法");
		//TODO 根据传入的User_code查找用户详细信息,,根据成功与否,执行相应的跳转
	}
	public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行update方法");
		//TODO 分页查询所有用户信息,根据成功与否,执行相应的跳转
	}
	public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行login方法");
		try {
			SetUserInfo(request, response);
			logger.debug("5.执行业务方法");
			userInfoService.login(userInfo);
			//登陆成功,写入信息到会话当中
			request.getSession(true).setAttribute("userInfo", userInfo);
			request.getRequestDispatcher("/pages/common/index.jsp").forward(request, response);
			
		} catch (ServiceException e) {
			logger.error("登陆失败:"+e.getMessage());
			request.setAttribute("message", e.getMessage());
			request.getRequestDispatcher("/").forward(request, response);
		}
	}
	public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行logout方法");
		request.getSession().invalidate();
		response.sendRedirect(request.getContextPath());
	}
	public void changePwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    logger.debug("执行changePwd方法");
		try {
	    	SetUserInfo(request, response);
			userInfoService.changePwd(userInfo.getUserCode(), userInfo.getUserPwd(), userInfo.getUserNewPwd1());
			request.setAttribute("message", "修改密码成功");
			request.setAttribute("statusCode", 200);
			request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
		} catch (Exception e) {
			logger.debug("修改密码失败:"+e.getMessage());
			request.setAttribute("message", "修改密码失败:"+e.getMessage());
			request.setAttribute("statusCode", 300);
			request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
		}
	}
}
